🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

php-parser

Package Overview
Dependencies
Maintainers
2
Versions
79
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

php-parser

Parse PHP code from JS and returns its AST

3.2.4
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
2
Weekly downloads
 
Created

What is php-parser?

The php-parser npm package is a JavaScript library that allows you to parse PHP code into an Abstract Syntax Tree (AST). This can be useful for various tasks such as static analysis, code transformation, and code generation.

What are php-parser's main functionalities?

Parsing PHP Code

This feature allows you to parse PHP code into an Abstract Syntax Tree (AST). The code sample demonstrates how to parse a simple PHP script and output the resulting AST.

const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');
console.log(JSON.stringify(ast, null, 2));

Traversing the AST

This feature allows you to traverse the AST to find specific nodes. The code sample demonstrates how to traverse the AST to find and log echo statements.

const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
const ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');

function traverse(node) {
  if (node.kind === 'echo') {
    console.log('Found an echo statement');
  }
  for (let key in node) {
    if (node[key] && typeof node[key] === 'object') {
      traverse(node[key]);
    }
  }
}

traverse(ast);

Modifying the AST

This feature allows you to modify the AST. The code sample demonstrates how to change the output of an echo statement from 'Hello, World!' to 'Hello, Universe!'.

const parser = require('php-parser');
const phpParser = new parser({ parser: { extractDoc: true } });
let ast = phpParser.parseCode('<?php echo "Hello, World!"; ?>');

function modifyEcho(node) {
  if (node.kind === 'echo') {
    node.arguments[0].value = 'Hello, Universe!';
  }
  for (let key in node) {
    if (node[key] && typeof node[key] === 'object') {
      modifyEcho(node[key]);
    }
  }
}

modifyEcho(ast);
console.log(JSON.stringify(ast, null, 2));

Other packages similar to php-parser

Keywords

php

FAQs

Package last updated on 27 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts